package com.usian.ai.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.ai.common.Result;
import com.usian.ai.common.vo.CandidateReqVo;
import com.usian.ai.common.vo.CandidateSaveReqVo;
import com.usian.ai.mapper.CandidateMapper;
import com.usian.ai.mapper.JobMapper;
import com.usian.ai.pojo.Candidate;
import com.usian.ai.pojo.Job;
import com.usian.ai.service.CandidateService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @TODO:
 * @Author:baimu
 * @Date:2024 12 05 09 46
 **/

@Service
public class CandidateServiceImpl extends ServiceImpl<CandidateMapper, Candidate>implements CandidateService {


    @Autowired
    CandidateMapper candidateMapper;

    @Autowired
    JobMapper jobMapper;
    @Override
    public Result list(CandidateReqVo candidateReqVo) {
        QueryWrapper<Candidate> candidateQueryWrapper = new QueryWrapper<>();
        candidateQueryWrapper.lambda().eq(StrUtil.isNotBlank(candidateReqVo.getRealName()),Candidate::getRealName,candidateReqVo.getRealName());
        candidateQueryWrapper.lambda().eq(StrUtil.isNotBlank(candidateReqVo.getMobile()),Candidate::getMobile,candidateReqVo.getMobile());
        IPage<Candidate> page = new Page<Candidate>(candidateReqVo.getPageNum(),candidateReqVo.getPageSize());
        IPage<Candidate> candidateIPage = candidateMapper.selectPage(page, candidateQueryWrapper);

//        candidateIPage.getRecords().forEach(item->{
//            Integer jobId = item.getJobId();
//            Job job = jobMapper.selectById(jobId);
//            String jobName = job.getJobName();
//            item.setJobName(jobName);
//        });



        //用jobID查询jobName；

//        List<Job> jobs = jobMapper.selectList(null);
//        candidateIPage.getRecords().forEach(item->{
//            jobs.forEach(job->{
//                Integer id = job.getId();
//                String jobName = job.getJobName();
//                if(item.getJobId().intValue()==id.intValue()){
//                    item.setJobName(jobName);
//                }
//
//            });
//        });

        List<Job> jobs = jobMapper.selectList(null);
        Map<Integer,String> jobMap = new HashMap<Integer,String>(16);

        jobs.forEach(job->{
            Integer id = job.getId();
            String jobName = job.getJobName();
            jobMap.put(id,jobName);
        });

        candidateIPage.getRecords().forEach(item->{
            Integer jobId = item.getJobId();
            String jobName = jobMap.get(jobId);
            item.setJobName(jobName);
        });


        return Result.ok(candidateIPage);
    }

    @Override
    public Result add(CandidateSaveReqVo candidateSaveReqVo) {

        Candidate candidate = new Candidate();
        candidate.setCreatedTime(new Date());
        candidate.setUpdatedTime(new Date());
        //对象复制
        BeanUtils.copyProperties(candidateSaveReqVo,candidate);

        int insert = candidateMapper.insert(candidate);

        return Result.ok(insert);
    }
}
